/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package heartinjava;

import java.io.*;
import java.net.*;

/**
 *
 * @author TIENDAT
 */

//-------------------------------------------------
//-- xu ly truyen va nhan cua cac client với server
//-------------------------------------------------

public class Client implements Runnable {
    
    private Socket socket = null;
    private Thread thread = null;
    private DataInputStream streamIn = null;
    private DataOutputStream streamOut = null;
    private HeartInJava m_Main ;
    public boolean m_Connect;
    
    public Client(String serverName, int serverPort,final HeartInJava m_main) 
    {
        System.out.println("Đang thiết lập kết nối. Vui lòng chờ ...");
        try {
            socket = new Socket(serverName, serverPort);
            System.out.println("Connected: " + socket);
            m_Main = m_main;
            beginNhanGui();
            m_Connect = true;
        } catch (UnknownHostException uhe) {
            System.out.println("Host unknown: " + uhe.getMessage());
            m_Connect = false;
        } catch (IOException ioe) {
            System.out.println("Unexpected exception: " + ioe.getMessage());
            m_Connect = false;
        }
    }
    
    public DataInputStream getStreamIn() 
    {
        return streamIn;
    }

    public void setStreamIn(DataInputStream streamIn) 
    {
        this.streamIn = streamIn;
    }
    
    //----------------------------------
    //-- run theard
    //----------------------------------
    public void run() {
        while (thread != null) {
            try {
                ThongDiepTuServer(streamIn.readUTF());
            } catch (IOException ioe) {
                System.out.println("Error: " + ioe.getMessage());
                stopThread();
            }
        }
    }

    //-----------------------------------
    //-- gởi dữ liệu đến sever
    //-----------------------------------
    public void SendDataToServer(String msg)
    {
        try{
            streamOut.writeUTF(msg);
            streamOut.flush();
        } catch (IOException ioe) {
            System.out.println("Sending error: " + ioe.getMessage());
            stopThread();        
        }
    }

    //------------------------------------
    // -- xử lý khi sever gởi thông báo tới
    //------------------------------------
    public void ThongDiepTuServer(String msg) 
    {
        if (msg.equals(".bye")) {
            System.out.println("Không ngày tái ngộ ...");
            stopThread();
        } else {
            m_Main.ThonDiepTuServer(msg);
        }
    }

    //--------------------------------------
    // -- khởi tạo việc nhận và gởi dữ liệu
    //--------------------------------------
    public void beginNhanGui() throws IOException 
    {
        // data nhận
        streamIn = new DataInputStream(socket.getInputStream());
        
        //data gởi
        streamOut = new DataOutputStream(socket.getOutputStream());
        if (thread == null) 
        {
            thread = new Thread(this);
            thread.start();
        }
    }

    //-----------------------------------
    // -- ngưng tiến trình
    //-----------------------------------
    public void stopThread() 
    {
        m_Connect = false;
        if (thread != null) {
            thread.stop();
            thread = null;
        }
        try {
            if (getStreamIn() != null) 
            {
                getStreamIn().close();
            }
            if (streamOut != null) 
            {
                streamOut.close();
            }
            if (socket != null) 
            {
                socket.close();
            }
        } catch (IOException ioe) {
            System.out.println("Lỗi ...");
        }
    }
}
